import {
  Api,
  MyHttpClient
} from "../api"
import {
  resolve
} from "upath";
import {
  rejects
} from "assert";
import cookie from "vue-cookies"

const cookieLangKey = "CurrentLanguage"


export default {
  name: "lang",
  namespaced: true,
  state: () => {
    return {
      rtl: true,
      language: null,
      select_language: null,
      initialize: null,
      name: null,
      next: null,
      previous: null,
      available_languages: null,
      initialize_mode_message: null,
      web_server_sync_mode: null,
      upload_local_data_mode: null,
      i_m_a_web_server_manualy_mode: null,
      age: null,
      id: null,
      label_of_uighur_language: null,
      label_of_kazakh_language: null,
      label_of_english_language: null,
      label_of_russian_language: null,
      label_of_chinese_language: null,
      email: null,
      qq_number: null,
      wechat_number: null,
      phone_number: null,
      task_running: null,
      please_select_a_database_engine: null,
      database: null,
      database_configuration: null,
      supported_database_engines: null,
      jdbc_driver: null,
      if_you_no_provide_jdbc_driver_then_this_is_not_required: null,
      initial_pool_size: null,
      max_pool_size: null,
      min_pool_size: null,
      user_name: null,
      password: null,
      jdbc_url: null,
      without_database_name: null,
      contain_database_name: null,
      database_name: null,
      prepare_create_database: null,
      step_prepare_create_database: null,
      prepare_connect_to_database: null,
      step_prepare_database_configuration: null,
      database_configuration_description: null,
      create_database_description: null,
      create_database: null,
      database_engine: null,
      must_be_without_database_name: null,
      must_be_contain_database_name: null,
      status: null,
      click_here_to_start: null,
      create_table: null,
      connect_to_database: null,
      successed: null,
      failed: null,
      go_to_settings_page: null,
      import_area_data_from_local: null,
      start: null,
      start_using: null,
      application: null,
      create_an_application: null,
      user: null,
      create_an_super_user: null,
      account: null,
      please_input_company_name: null,
      please_input_company_description: null,
      contacts: null
    }
  },
  getters: {
    flex_direction: state => state.rtl ? "row-reverse" : "row"
  },
  mutations: {
    updateObject(state, {
      langCode,
      data
    }) {
      updateResources(state, data)
    }
  },
  actions: {

  },

  modules: {
    management: {
      namespaced: true,
      state: {
        current: cookie.get(cookieLangKey) || "ug",
        available: new Array()
      },
      mutations: {
        setAvailable(state, data) {
          state.available = data
        },
        setCurrent(state, langCode) {
          state.current = langCode
        }
      },
      actions: {
        listLanguages(context) {
          return new Promise((resolve, rejects) => {
            new MyHttpClient().get(Api.languageList()).then(data => {
              context.commit("setAvailable", data.data)
              resolve(data.data)
            }).catch(exception => {
              rejects(exception)
            })
          })
        },

        changeLanguage(context, langCode) {
          return new Promise((resolve, rejects) => {
            let expire = new Date()
            expire.setFullYear(expire.getFullYear() + 1)

            cookie.set(cookieLangKey, langCode, expire)

            new MyHttpClient().get(Api.languageStrings(langCode)).then(success => {
              this.commit("lang/updateObject", {
                langCode,
                data: success.data
              })
              context.commit("setCurrent", langCode)
              resolve(true)
            }).catch(error => {
              console.log(error)
              rejects(error)
            })
          })

        },
        async loadDefault({
          dispatch,
          state
        }) {
          return dispatch("changeLanguage", state.current)
        }
      }
    }
  }
}


function updateResources(strings, data) {
  strings.name = data.name
  strings.rtl = data.rtl
  strings.select_language = data.select_language
  strings.initialize = data.initialize
  strings.language = data.language
  strings.next = data.next
  strings.previous = data.previous
  strings.available_languages = data.available_languages
  strings.initialize_mode_message = data.initialize_mode_message
  strings.web_server_sync_mode = data.web_server_sync_mode
  strings.upload_local_data_mode = data.upload_local_data_mode
  strings.i_m_a_web_server_manualy_mode = data.i_m_a_web_server_manualy_mode
  strings.age = data.age
  strings.id = data.id
  strings.label_of_chinese_language = data.label_of_chinese_language
  strings.label_of_english_language = data.label_of_english_language
  strings.label_of_kazakh_language = data.label_of_kazakh_language
  strings.label_of_russian_language = data.label_of_russian_language
  strings.label_of_uighur_language = data.label_of_uighur_language
  strings.email = data.email
  strings.phone_number = data.phone_number
  strings.qq_number = data.qq_number
  strings.wechat_number = data.wechat_number
  strings.task_running = data.task_running
  strings.please_select_a_database_engine = data.please_select_a_database_engine
  strings.database = data.database
  strings.database_configuration = data.database_configuration
  strings.supported_database_engines = data.supported_database_engines
  strings.jdbc_driver = data.jdbc_driver
  strings.if_you_no_provide_jdbc_driver_then_this_is_not_required = data.if_you_no_provide_jdbc_driver_then_this_is_not_required
  strings.initial_pool_size = data.initial_pool_size
  strings.max_pool_size = data.max_pool_size
  strings.min_pool_size = data.min_pool_size
  strings.user_name = data.user_name
  strings.password = data.password
  strings.jdbc_url = data.jdbc_url
  strings.without_database_name = data.without_database_name
  strings.contain_database_name = data.contain_database_name
  strings.database_name = data.database_name
  strings.prepare_create_database = data.prepare_create_database
  strings.database_configuration_description = data.database_configuration_description
  strings.create_database_description = data.create_database_description
  strings.prepare_connect_to_database = data.prepare_connect_to_database
  strings.step_prepare_create_database = data.step_prepare_create_database
  strings.step_prepare_database_configuration = data.step_prepare_database_configuration
  strings.create_database = data.create_database
  strings.database_engine = data.database_engine
  strings.must_be_contain_database_name = data.must_be_contain_database_name
  strings.must_be_without_database_name = data.must_be_without_database_name
  strings.status = data.status
  strings.click_here_to_start = data.click_here_to_start
  strings.create_table = data.create_table
  strings.connect_to_database = data.connect_to_database
  strings.successed = data.successed
  strings.failed = data.failed
  strings.go_to_settings_page = data.go_to_settings_page
  strings.import_area_data_from_local = data.import_area_data_from_local
  strings.start = data.start
  strings.start_using = data.start_using
  strings.application = data.application
  strings.create_an_application = data.create_an_application
  strings.user = data.user
  strings.create_an_super_user = data.create_an_super_user
  strings.account = data.account
  strings.please_input_company_name = data.please_input_company_name
  strings.please_input_company_description = data.please_input_company_description
  strings.contacts = data.contacts
}
